package com.one.ui;

import com.one.component.BookManageComponent;
import com.one.domain.ResultInfo;
import com.one.net.GetUtils;
import com.one.utils.JsonUtils;
import com.one.utils.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class ManagerInterface {
    JFrame jf = new JFrame("图书馆: Xxx, 欢迎您");
    final int WIDTH = 1000;
    final int HEIGHT = 600;
    // 组装视图
    public void init() throws IOException {
        // 给窗口设置属性
        jf.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);
        jf.setResizable(false);
        jf.setIconImage(ImageIO.read(new File("img/logo.png")));
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置菜单栏
        JMenuBar jmb =new JMenuBar();
        JMenu jMenu =new JMenu("设置");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");
        jMenu.add(m1);
        jMenu.add(m2);
        jmb.add(jMenu);
        m1.addActionListener(e -> {
            try {
                new MainInterface().init();
                jf.dispose();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        m2.addActionListener(e -> {
            System.exit(0);
        });
        jf.setJMenuBar(jmb);

        // 设置分割面板
        JSplitPane sp =new JSplitPane();
        // 支持连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(150);
        sp.setDividerSize(7);
        // 左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统管理");
        DefaultMutableTreeNode userManage = new DefaultMutableTreeNode("用户管理");
        DefaultMutableTreeNode bookManage = new DefaultMutableTreeNode("图书管理");
        DefaultMutableTreeNode borrowManage = new DefaultMutableTreeNode("借阅管理");
        DefaultMutableTreeNode  statisticalManage = new DefaultMutableTreeNode("统计分析");
        root.add(userManage);
        root.add(bookManage);
        root.add(borrowManage);
        root.add(statisticalManage);

        // 将结点添加至树中
        JTree tree = new JTree(root);
        Color color =new Color(203,220,217);
        MyRenderer myRenderer =new MyRenderer();
        myRenderer.setBackgroundNonSelectionColor(color);
        myRenderer.setBackgroundSelectionColor(new Color(140,140,140));
        tree.setCellRenderer(myRenderer);
        tree.setBackground(color);
        // 设置当前tree默认选中
        tree.setSelectionRow(2);
        tree.addTreeSelectionListener(e -> {
            // 当条目选中变化后,此方法执行,得到选中当前结点路径
            Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();
            if (userManage.equals(lastPathComponent)) {
                sp.setRightComponent(new JLabel("这里进行用户管理"));
                sp.setDividerLocation(150);
            } else if (bookManage.equals(lastPathComponent)) {
                sp.setRightComponent(new BookManageComponent(jf));
                sp.setDividerLocation(150);
            } else if (borrowManage.equals(lastPathComponent)) {
                sp.setRightComponent(new JLabel("这里进行借阅管理"));
                sp.setDividerLocation(150);
            } else if (statisticalManage.equals(lastPathComponent)) {
                sp.setRightComponent(new JLabel("这里进行统计分析"));
                sp.setDividerLocation(150);
            }
        });

        sp.setLeftComponent(tree);
        sp.setRightComponent(new BookManageComponent(jf));
        jf.add(sp);
        jf.setVisible(true);

        GetUtils.get("http://localhost:8080/user/findUsername",result -> {
            ResultInfo info = JsonUtils.parseResult(result);
            if (info.isFlag()) {
                jf.setTitle("图书馆: "+info.getData()+"欢迎您");
            }
        },() -> {

        });
    }
    public static void main(String[] args) throws IOException {
        new ManagerInterface().init();
    }

    // 自定义结点绘制器
    private class MyRenderer extends DefaultTreeCellRenderer {
        private ImageIcon rootIcon = null;
        private ImageIcon userManageIcon = null;
        private ImageIcon bookManageIcon = null;
        private ImageIcon borrowManageIcon = null;
        private ImageIcon statisticManageIcon = null;
        public MyRenderer() {
                rootIcon = new ImageIcon("img/systemManage.png");
                userManageIcon = new ImageIcon("img/userManage.png");
                bookManageIcon = new ImageIcon("img/bookManage.png");
                borrowManageIcon = new ImageIcon("img/borrowManage.png");
                statisticManageIcon = new ImageIcon("img/statisticsManage.png");
        }
        // 当绘制树的每个结点时,都会调用这个方法
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            // 使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            ImageIcon image= null;
            switch (row) {
                case 0:
                    image = rootIcon;
                    break;
                case 1:
                    image = userManageIcon;
                    break;
                case 2:
                    image = bookManageIcon;
                    break;
                case 3:
                    image = borrowManageIcon;
                    break;
                case 4:
                    image = statisticManageIcon;
                    break;
            }
            this.setIcon(image);
            return this;

        }
    }
}
